package com.redxiii.spike.pmi.command;

import java.util.Date;
import java.util.Iterator;
import java.util.SortedSet;

import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.redxiii.spike.api.command.CommandFactory;
import com.redxiii.spike.api.command.InitializationRequired;
import com.redxiii.spike.api.pmi.entity.Issue;
import com.redxiii.spike.util.message.BasicMsgInterpreter;

/**
 * @author Daniel Filgueiras
 * 17/11/2011
 */
@InitializationRequired
@CommandFactory(BasicMsgInterpreter.class)
public class TodayIssues extends ShowIssues {

	private static final Logger logger = LoggerFactory.getLogger(TodayIssues.class);
	
	@Override
	protected SortedSet<Issue> filterIssues(SortedSet<Issue> issues) {
		
		issues = super.filterIssues(issues);
		
		Date today = new DateTime().withMillisOfDay(0).toDate();
		
		Iterator<Issue> iterator = issues.iterator();
		while (iterator.hasNext()) {
			Issue issue = iterator.next();
			
			if (issue.getCreatedDate().before(today)) {
				logger.debug("Issue id {} discarted", issue.getId());
				iterator.remove();
			}
		}
		
		return issues;
	}
}
